;(function() {
	
		var getFormTemplate = function (options) {
		
		//		{
		//			formId: "",
		//			fInputId : "",
		//			fInputName : "",
		//			sInputId : "",
		//			sInputName : "",
		//			submitId : ""
		//		}
		var opt = options
		return `<form id="${opt.formId}">
					<div class="input-group">
					<input type="hidden" class="form-control"
					id="${opt.fInputId}" name="${opt.fInputName}">
					<input type="text"
					class="form-control" id="${opt.sInputId}" name="${opt.sInputName}"
					placeholder="请输入...">
				<span class="input-group-btn">
					<button class="btn btn-default" type="button"
						id="${opt.submitId}">Submit!</button>
				</span>
			</div>
			</form>`;
	}
	var getBtnTemplate = function (options) {
		
		var opt = options
		return 	`<div class="modal-footer">
		<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
		<button type="button" class="btn btn-primary">Save changes</button>
	</div>`;
	}
	var getModelTemplate = function (options) {
		
		// {
		// modalId : "rename-modal",
		// modalTitle : "重命名",
		// modalBody: renameBodyTmp,
		// modalFooter : "",
		// }
		var opt = options;
		return `
		<div id="${opt.modalId}" class="modal fade" tabindex="-1" role="dialog">
			<div class="modal-dialog" role="document">
				<div class="modal-content">
					<div class="modal-header">
						<button type="button" class="close" data-dismiss="modal"
							aria-label="Close">
							<span aria-hidden="true">&times;</span>
						</button>
						<h4 class="modal-title" id="${opt.modalTitleId}" >${opt.modalTitle}</h4>
					</div>
					<div class="modal-body" id="${opt.modalBodyId}">
						${opt.modalBody}
					</div>
					${opt.modalFooter}
				</div>
			</div>
		</div>`;
	}
	
	var initModel = function(){
		initRenameModel();
		initCopyModel();
		initEditModel();
	}
	
	var initRenameModel = function(){
		var formOptions = {
				formId: "rename-form",
				fInputId : "f-rename-input",
				fInputName : "name",
				sInputId : "s-rename-input",
				sInputName : "rename",
				submitId : "rename-submit"
			};
		var renameBodyTmp = getFormTemplate(formOptions);
		var modalOptions =  {
				modalId : "rename-modal",
				modalTitleId : "rename-title-id",
				modalTitle : "重命名",
				modalBodyId : "rename-body-id",
				modalBody: renameBodyTmp,
				modalFooter : "",
			};
		
		var renameModelTmp = getModelTemplate(modalOptions);
		$("body").append(renameModelTmp);
	}
	var initCopyModel = function(){
		var formOptions = {
				formId: "copy-form",
				fInputId : "f-copy-input",
				fInputName : "name",
				sInputId : "s-copy-input",
				sInputName : "targetDir",
				submitId : "copy-submit"
			};
		var copyBodyTmp = getFormTemplate(formOptions);
		var modalOptions =  {
				modalId : "copy-modal",
				modalTitleId : "copy-title-id",
				modalTitle : "复制",
				modalBodyId : "copy-body-id",
				modalBody: copyBodyTmp,
				modalFooter : "",
			};
		
		var copyModelTmp = getModelTemplate(modalOptions);
		$("body").append(copyModelTmp);
	}

	var initEditModel = function(){
		
		var editFooterTmp = `<div id="edit-footer-id" class="modal-footer">
			<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
			<button id="edit-submit" type="button" class="btn btn-primary">提交更改</button>
		</div>`;

		var editBodyTmp = `<textarea style="resize: none;" class="form-control" rows="20"
		  id="edit-textarea" readonly>不是文件！</textarea>`;
		var modalOptions =  {
				modalId : "edit-modal",
				modalTitleId : "edit-title-id",
				modalTitle : "查看",
				modalBodyId : "edit-body-id",
				modalBody: editBodyTmp,
				modalFooter : editFooterTmp,
			};
		
		var editModelTmp = getModelTemplate(modalOptions);
		$("body").append(editModelTmp);
	}
	
	

	var fileContentUrl = "file/content";
	var fileRenameUrl = "file/rename";
	var fileCopyUrl = "file/copy";
	var fileDeleteUrl = "file/delete";
	var fileDownloadUrl = "files/download";
	var fileContentEditorUrl = "file/content/editor";
	
	// 获取文件的全路径
	var getCurrentName = function(name) {
		return currentDir =="" ? name : currentDir + "/" + name;
	}
	// 获取文件的父路径
	var getParentName = function (name){
		var parentName = "";
		var i = name.lastIndexOf("/");
		if(i != -1){
			parentName = name.substring(0,i);
		};
		return parentName
	}
	window.operateEvents = {
			
			"click #show" : function(e, value, row, index) {
				$('#edit-modal').on('show.bs.modal', function(e) {
					
					var name = getCurrentName(row.name);
					log(row.fileType);
					if (row.fileType != 1 ) return false;
					$.post(fileContentUrl,{
						"name" : name,
					}, function(value) {
						//console.log(value.message)
						$('#edit-title-id').text(row.name);
						// TODO 此处应先将其格式化
						
						$('#edit-textarea').text(value.message);
						$('#edit-textarea').attr("readonly","readonly");
						$('#edit-submit').hide();
					});
				});
				//console.log($('#edit-modal'))
				
				$('#edit-modal').modal();
			},
			"click #edit" : function(e, value, row, index) {
				var name =getCurrentName(row.name);
				$('#edit-modal').on('show.bs.modal',function(e) {
					$.ajax({
							type : "post",
							url : fileContentUrl,
							data : {
								"name" : name,
								type : row.fileType
							},
							success : function(value) {
								// console.log(value)
								$('#edit-title-id').text(row.name);
								$('#edit-textarea').removeAttr("readonly");
								$('#edit-textarea').text(value.message);
								$('#edit-submit').show();
							}
						});
				});
				$('#edit-modal').modal()
			},
			"click #rename" : function(e, value, row, index) {

				var name =getCurrentName(row.name);
				$('#f-rename-input').val(name);
				$('#s-rename-input').val(name);
				$('#rename-modal').modal()
			},
			"click #copy" : function(e, value, row, index) {
				var name = getCurrentName(row.name);
				var parentName = getParentName(name);
				log("parentName",parentName)
				$('#f-copy-input').val(name);
				$('#s-copy-input').val(parentName);
				$('#copy-modal').modal();
				
			},
			"click #delete" : function(e, value, row, index) {
				var name = getCurrentName(row.name);
				
				$.get(fileDeleteUrl , {"name" : name}, function(result){

					 enterDir(currentDir);
				 });
			},
			"click #download" : function(e, value, row, index) {
				//如果下载文件夹，直接返回（暂不支持）
				if (row.fileType != 1 ) return false;
				var name = getCurrentName(row.name);
				
//			    var tt =newDate().getTime();
			    var url = fileDownloadUrl;
			    var form=$("<form>");//定义一个form表单
			    form.attr("style","display:none");
			    form.attr("target","");
			    form.attr("method","get");//请求类型
			    form.attr("action",url);//请求地址
			    $("body").append(form);//将表单放置在web中

//			    var input1=$("<input>");
//			    input1.attr("type","hidden");
//			    input1.attr("name","tt");
//			    input1.attr("value",tt);
//			    form.append(input1);

			    var input2=$("<input>");
			    input2.attr("type","hidden");
			    input2.attr("name","name");
			    input2.attr("value",name);
			    form.append(input2);
			    form.submit();//表单提交
			
			},
		}
	
	initModel();
	$("#copy-submit").on("click", function () {
		$.getJSON( fileCopyUrl , $('#copy-form').serialize() ,function(result){
			
			if (result.code == 200) {
        	    $('#copy-modal').modal('hide');
        	    enterDir(currentDir);
			}else{
				alert(result.message);
			}
		});
	});
	$("#rename-submit").on("click", function () {
		$.getJSON( fileRenameUrl ,$('#rename-form').serialize(),function(result){
			
			if (result.code == 200) {
        	    $('#rename-modal').modal('hide');
        	    enterDir(currentDir);
			}else{
				alert(result.message);
			}
		});
	});

	$("#edit-submit").on("click", function () {

		var textarea = $('#edit-textarea').val();
		var title = $('#edit-title-id').text();
		var name = getCurrentName(title);
		$.ajax({
		  // cache:true,// 保留缓存数据
		  type:"post",
		  url:fileContentEditorUrl,// 这是我在后台接受数据的文件名
		  data:{text: textarea, "name": name},
		  async: false,// 设置成true，这标志着在请求开始后，其他代码依然能够执行。如果把这个选项设置成false，这意味着所有的请求都不再是异步的了，这也会导致浏览器被锁死
		  error:function(request){// 请求失败之后的操作
			  console.log("error");
		  },
		  success:function(data){// 请求成功之后的操作
		      console.log("success");
		      $('#edit-modal').modal('hide');  
		  }
		});
	});
})();


